package indigo.mybatis.dao;

import static java.util.Objects.requireNonNull;

import java.io.Serializable;

public class Projection implements Serializable {
	private static final long serialVersionUID = -1159168213654865922L;

	private final String property;
	private final Mode mode;

	public Projection(String property, Mode mode) {
		this.property = requireNonNull(property, "property cannot be null");
		this.mode = requireNonNull(mode, "mode cannot be null");
	}

	public String getProperty() {
		return property;
	}

	public Mode getMode() {
		return mode;
	}

	@Override
	public String toString() {
		return String.format("Projection {property=%s, mode=%s}", property, mode.name());
	}

	public static enum Mode {
		PROPERTY, DISTINCT, COUNT, COUNT_DISTINCT, MAX, MIN, SUM, AVG
	}

	public static Projection property(String property) {
		return new Projection(property, Mode.PROPERTY);
	}

	public static Projection distinct(String property) {
		return new Projection(property, Mode.DISTINCT);
	}

	public static Projection count(String property) {
		return new Projection(property, Mode.COUNT);
	}

	public static Projection countDistinct(String property) {
		return new Projection(property, Mode.COUNT_DISTINCT);
	}

	public static Projection max(String property) {
		return new Projection(property, Mode.MAX);
	}

	public static Projection min(String property) {
		return new Projection(property, Mode.MIN);
	}

	public static Projection sum(String property) {
		return new Projection(property, Mode.SUM);
	}

	public static Projection avg(String property) {
		return new Projection(property, Mode.AVG);
	}
}
